﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SmartSpace.Core;
using SmartSpace.Workflow;
using SsTool.Common;
using System.Collections;

namespace SsTool.Workflow.Windows
{
    /// <summary>
    /// Interaction logic for UpdateEventWindow.xaml
    /// </summary>
    public partial class UpdateEventWindow : Window
    {
        public EventMetadata EventMetadata { get; set; }

        public UpdateEventWindow(List<ActivityMetadata> activities, ActivityMetadata activity)
        {
            InitializeComponent();

            EventMetadata = new EventMetadata();
            EventMetadata.Id = Guid.NewGuid();
            EventMetadata.Caption = string.Format("新事件{0}",EventMetadata.Id);
            if (activity.Events != null)
            {
                EventMetadata.OrderIndex = activity.Events.Count;
            }
            EventMetadata.FormActivity = activity;
            EventMetadata.FormActivityId = activity.Id;
            EventMetadata.Rights = new List<EventRight>();

            foreach (var activityRight in EventMetadata.FormActivity.Rights)
            {
                EventRight er = new EventRight();
                er.Id = Guid.NewGuid();
                er.Event = EventMetadata;
                er.EventId = EventMetadata.Id;
                er.Role = activityRight.Role;
                er.RoleId = activityRight.RoleId;
                EventMetadata.Rights.Add(er);
            }

            _ctlGridActivityRights.ItemsSource = EventMetadata.FormActivity.Rights;
            _ctlGridRights.ItemsSource = EventMetadata.Rights;
            this.DataContext = EventMetadata;
            comboBox1.ItemsSource = activities;
        }

        public UpdateEventWindow(List<ActivityMetadata> activities, EventMetadata metadata)
        {
            InitializeComponent();

            EventMetadata = metadata;

            if (EventMetadata.Rights.Count == 0)
            {
                foreach (var activityRight in EventMetadata.FormActivity.Rights)
                {
                    EventRight er = new EventRight();
                    er.Id = Guid.NewGuid();
                    er.Event = EventMetadata;
                    er.EventId = EventMetadata.Id;
                    er.Role = activityRight.Role;
                    er.RoleId = activityRight.RoleId;
                    EventMetadata.Rights.Add(er);
                }
            }

            _ctlGridActivityRights.ItemsSource = EventMetadata.FormActivity.Rights;
            _ctlGridRights.ItemsSource = EventMetadata.Rights;

            this.DataContext = EventMetadata;
            comboBox1.ItemsSource = activities;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (EventMetadata.ToActivity != null)
            {
                EventMetadata.ToActivityId = EventMetadata.ToActivity.Id;
            }

            DialogResult = true;
        }

        private void _ctlBtnSelectEventType_Click(object sender, RoutedEventArgs e)
        {
            SelectEventTypeWindow window = new SelectEventTypeWindow();
            if (window.ShowDialog() == true)
            {
                EventMetadata.Type = window.SelectedEventType;
                EventMetadata.TypeId = EventMetadata.Type.Id;

                this.DataContext = null;
                this.DataContext = EventMetadata;
            }
        }

        private void _ctlBtnAdd_Click(object sender, RoutedEventArgs e)
        {
            IList items = _ctlGridActivityRights.SelectedItems as IList;
            if (items != null && items.Count > 0)
            {
                foreach (var item in items)
                {
                    var activityRight = item as ActivityRight;

                    bool isExist = false;
                    foreach (var eventRight in EventMetadata.Rights)
                    {
                        if (eventRight.RoleId == activityRight.RoleId)
                        {
                            isExist = true;
                            break;
                        }
                    }

                    if (!isExist)
                    {
                        EventRight er = new EventRight();
                        er.Id = Guid.NewGuid();
                        er.Event = EventMetadata;
                        er.EventId = EventMetadata.Id;
                        er.Role = activityRight.Role;
                        er.RoleId = activityRight.RoleId;
                        EventMetadata.Rights.Add(er);
                    }
                }

                _ctlGridRights.ItemsSource = null;
                _ctlGridRights.ItemsSource = EventMetadata.Rights;
            }
        }

        private void _ctlBtnRemove_Click(object sender, RoutedEventArgs e)
        {
            IList items = _ctlGridRights.SelectedItems as IList;
            if (items != null && items.Count > 0)
            {
                foreach (var item in items)
                {
                    EventRight eventRight = item as EventRight;
                    EventMetadata.Rights.Remove(eventRight);
                }

                _ctlGridRights.ItemsSource = null;
                _ctlGridRights.ItemsSource = EventMetadata.Rights;
            }
        }
    }
}
